package soar.test.json.fastjson;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.SerializerFeature;

public class App
{
    public static void main(String args[])
    {
        new App().run();
    }

    private void run()
    {
        try
        {
            String split = "==================================================";
            System.out.println(split);

            generate();
            System.out.println(split);

            parse();
            System.out.println(split);

            // 多态
            new Polymorphic().run();
            System.out.println(split);
        }
        catch (Exception e)
        {
            System.out.println(e);
        }
    }

    private String input_s;

    private void generate() throws Exception
    {
        System.out.println("对象转换");
        System.out.println();

        String out_s;

        Map<String, String> map = new HashMap<>();
        map.put("姓名", "甲");
        map.put("地址", "上海");
        map.put("爱好", "电影");
        out_s = JSON.toJSONString(map);
        System.out.println(out_s);

        System.out.println();

        List<SimpleUser> list = new ArrayList<>();
        SimpleUser su = new SimpleUser();
        su.id = 100;
        su.name = "甲";
        list.add(su);
        su = new SimpleUser();
        su.id = 200;
        su.name = "乙";
        list.add(su);
        su = new SimpleUser();
        su.id = 300;
        su.name = "丙";
        list.add(su);
        out_s = JSON.toJSONString(list);
        System.out.println(out_s);

        System.out.println();

        User user = new User();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        user.id = 1;
        user.name = "阿隆";
        user.salary.put("月薪", 10000);
        user.salary.put("绩效", 2000);
        user.logs.add(new Log(1, format.parse("2010-01-01 09:30:13"), "入职"));
        user.logs.add(new Log(2, format.parse("2012-05-01 14:10:20"), "升职经理"));
        out_s = JSON.toJSONString(user);
        System.out.println(out_s);
        out_s = JSON.toJSONString(user,
                SerializerFeature.WriteDateUseDateFormat);
        System.out.println(out_s);
        out_s = JSON.toJSONStringWithDateFormat(user,
                "yyyy-MM-dd HH:mm:ss.SSS");
        System.out.println(out_s);

        input_s = out_s;
    }

    private void parse() throws Exception
    {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        System.out.println("数据绑定模式");
        System.out.println();

        System.out.println("Map");
        String map_s = "{\"甲\" : 100,\"乙\" : 200,\"丙\" : 300}";
        Map<String, Integer> map = JSON.parseObject(map_s,
                new TypeReference<Map<String, Integer>>()
                {
                });
        map.forEach((k, v) -> {
            System.out.println(k + " : " + v);
        });
        System.out.println();

        System.out.println("简单类");
        String simple_input_s = "{\"id\" : 1,\"name\" : \"阿隆\",\"age\" : 40}";
        SimpleUser su = JSON.parseObject(simple_input_s, SimpleUser.class);
        System.out.println("id: " + su.id);
        System.out.println("name: " + su.name);
//        simple_input_s = "{\"id\" : null,\"name\" : null,\"age\" : 40}";
//        su = JSON.parseObject(simple_input_s, SimpleUser.class);
//        System.out.println("id: " + su.id);
//        System.out.println("name: " + su.name);
        System.out.println();

        System.out.println("复杂类");
        User user = JSON.parseObject(input_s, User.class);
        System.out.println("id: " + user.id);
        System.out.println("name: " + user.name);
        System.out.println("salary:");
        user.salary.forEach(
                (k, v) -> System.out.println("    " + k + " --> " + v));
        System.out.println("logs:");
        user.logs.forEach((e) -> System.out.println("    " + " id:" + e.id
                + " time:" + format.format(e.time) + " content:" + e.content));
    }
}

class SimpleUser
{
    public int id = 0;
    public String name = "";
}

class User
{
    public int id;
    public String name;
    public Map<String, Integer> salary = new HashMap<>();
    public List<Log> logs = new ArrayList<>();
}

class Log
{
    public int id;
    public Date time;
    public String content;

    // 如定制构造方法，必须提供缺省构造方法
    public Log()
    {

    }

    public Log(int id, Date time, String content)
    {
        this.id = id;
        this.time = time;
        this.content = content;
    }
}
